DECLARE @intProductId INT
DECLARE @intBillDetail INT
DECLARE @intInputPrice INT

DECLARE CUR_ CURSOR LOCAL FAST_FORWARD FOR 
SELECT ProductId, Id FROM BillDetail
WHERE InputPrice IS NULL OR InputPrice = 0
OPEN CUR_
 
FETCH NEXT FROM CUR_
INTO @intProductId, @intBillDetail  

WHILE @@FETCH_STATUS = 0
BEGIN
	
	SELECT @intInputPrice = LatestInputPrice
	FROM Product
	WHERE Id = @intProductId
	
	PRINT @intBillDetail
	
	IF (@intInputPrice IS NOT NULL AND @intInputPrice > 0) BEGIN
		UPDATE BillDetail
		SET InputPrice = @intInputPrice
		WHERE Id = @intBillDetail
	END
	FETCH NEXT FROM CUR_ INTO @intProductId, @intBillDetail

END -- END WHILE

CLOSE CUR_
DEALLOCATE CUR_